package com.zmm.member.controller;

import com.zmm.common.annotation.SysLog;
import com.zmm.common.base.model.Result;
import com.zmm.common.dict.enums.RegionType;
import com.zmm.common.dict.enums.RegionVersion;
import com.zmm.common.dict.model.Region;
import com.zmm.common.pre.handle.Assert;
import com.zmm.member.handler.RegionHandler;
import com.zmm.member.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * @author 900045
 * @description:
 * @name AddressController
 * @date By 2021-08-04 11:52:18
 */
@Controller
@Slf4j
@RequestMapping("/address")
public class AddressController {
	
	@Resource
	private RegionHandler regionHandler;
	
	@Resource
	private DictService dictService;

	/**
	 * 初始化地址
	 * @author: 900045
	 * @date: 2021-08-04 11:56:19
	 * @throws 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@ResponseBody
	@GetMapping(value = "/init")
	public Result initializeAddress(){
		regionHandler.doSyncFromDb();
		return Result.ok("地址初始化成功");
	}

	/**
	 * 地址信息加载
	 * @author: 900045
	 * @date: 2021-08-04 14:37:24
	 * @throws 
	 * @param type: 类型:PROVINCE,CITY,DISTRICT,TOWN
	 * @param parent: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@SysLog(value = "查询地址", location = "regionReload", service = "service-member")
	@ResponseBody
	@RequestMapping(value = "/reload/list", headers = "version=2.0", method = RequestMethod.GET)
	public Result regionReload(@RequestParam String type, Integer parent) {
		RegionType regionType = RegionType.fetch(type);
		Assert.notNull(regionType);
		//Long startTime = DateUtil.getNowTime()
		//log.info("开始查询地址信息....[{}]", startTime)
		Collection<Region> regionList = dictService.queryRegionReload(RegionVersion.V2,regionType,parent);
		//log.info("查询地址信息结束....[{}]", DateUtil.getNowTime() - startTime)
		return Result.ok().setData(regionList);
	}
}
